//触发隐藏的file表单
function makeThisfile() {
  $("#image").click();
}

//file表单选中文件时,让file表单的val展示到showname这个展示框
$("#image").change(function () {
  $("#showname").val($(this).val());
});

//提交表单验证
$("#form").submit(function (e) {
  if ($("#wenjianjia").val() === null) {
    e.preventDefault();
    alert("请选择相册！");
  } else if ($("#image").val() == "") {
    e.preventDefault();
    alert("图片不能为空！");
  }
});
//点击图片展示
window.onload = function () {
  var box = document.querySelector("#box"),
    images = box.querySelectorAll(".thumbnail"),
    imgbox = document.querySelector("#imgbox"),
    showimg = document.querySelector(".showimg"),
    close = document.querySelector("#close"),
    imgLength = images.length;
  for (var i = 0; i < imgLength; i++) {
    images[i].onclick = function (e) {
      imgbox.style.display = "block";
      showimg.children[0].src = this.children[0].src;
    };
  }

  close.onclick = function () {
    
    imgbox.style.display = "none";
  };
};
//鼠标右键菜单函数
function showMenu(e) {
  oMenu.style.height = 120 + "px";
  //取消默认的浏览器自带右键 很重要！！
  e.preventDefault();
  //获取我们自定义的右键菜单
  var menu = document.querySelector("#menu");
  //根据事件对象中鼠标点击的位置，进行定位
  menu.style.left = e.clientX + "px";
  menu.style.top = e.clientY + "px";
  //改变自定义菜单的宽，让它显示出来
  menu.style.width = "125px";
}

//获取dom元素
var oMenu = document.querySelector("#menu"),
  oList = document.querySelector("#box").children,
  listLength = oList.length,
  menuList = oMenu.children,
  menuLength = menuList.length,
  filedir = document.querySelector(".active").innerText.trim();
var filename = "";
for (i = 0; i < listLength; i++) {
  oList[i].addEventListener(
    "mouseenter",
    function (e) {
      filename = this.children[0].title;
    },
    false
  );
}
//右键菜单点击逻辑处理
for (let i = 0; i < menuLength; i++) {
  menuList[i].addEventListener("click", function () {
    if (i == 0) {
      showUpLoad.call(document.querySelector(".add"));
    } else if (i == 1) {
      alert("功能研发中！！！");
    } else if (i == 2) {
      if (filename == "") {
        alert("您没有选择要删除的图片！");
        return false;
      }
      show_confirm(filename, filedir);
    }
  });
}
//右键显示菜单
window.oncontextmenu = function (e) {
  showMenu(e);
};
//关闭右键菜单
window.onclick = function (e) {
  oMenu.style.height = 0;
};
//确认删除
function show_confirm(filename, filedir) {
  var r = confirm("确定删除？");
  if (r == true) {
    removeFile(filename, filedir);
  } else {
    return false;
  }
}
//删除图片ajax请求
function removeFile(name, dir) {
  $.post("/removefile", { filename: name, filedir: dir }, function (data) {
    if (data.code == 0) {
      window.location.reload();
    }
  });
}
var upload = document.querySelector("#upload");

function showUpLoad() {
  $("#openModal").click()
}
